package com.sykj.util.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 李丛旺
 * @description
 * @date 2020/8/10
 */
@ServerEndpoint("/websocket/{id}")
@Component
public class WebSocket {


    private String id;///记录订单号
    private Session session;//当前的连接

    private static Map<String, Session> allClients = new ConcurrentHashMap<>();//用于存放所有订单和 websocket 连接的 map


    public static Map<String, Session> getAllClients() {
        return allClients;
    }



    /**
     * 当简历连接的时候调用
     *
     * @param id
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam("id") String id, Session session) {

        this.id = id;
        this.session = session;
        allClients.put(id, session);
    }

    @OnClose
    public void onClose(Session session,@PathParam(value = "id") String id){
           allClients.remove(id);

    }


}
